
package com.material.c2c.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtils {

  public static void copyBeanNotNull2Bean(Object source, Object target) {
    PropertyDescriptor sourceDescriptors[] = PropertyUtils.getPropertyDescriptors(source);
    for (int i = 0; i < sourceDescriptors.length; i++) {
      String name = sourceDescriptors[i].getName();
      if ("class".equals(name)) {
        continue; // No point in trying to set an object's class
      }
      if (PropertyUtils.isReadable(source, name) && PropertyUtils.isWriteable(target, name)) {
        try {
          Object value = PropertyUtils.getSimpleProperty(source, name);
          if (value != null) {
            PropertyUtils.setSimpleProperty(target, name, value);
          }
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        } catch (NoSuchMethodException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
